//
//  EditViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/28.
//

#import "EditViewController.h"
#import "MineModel.h"
#import "EditTableViewCell.h"
#import "ImagePickerManager.h"

@interface EditViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *editTV;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) ImagePickerManager *pickManager;

@end

@implementation EditViewController

- (void)initDidLoad {
    self.navigationTitle = @"编辑资料";
}

- (void)initViews {
    [self.view addSubview:self.editTV];
    [self.editTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(15, 0, 0, 0));
    }];
}

- (void)initRequestData {
    MineModel *model = [[MineModel alloc] init];
    model.title = @"头像";
    model.detailStr = kUserAccount.user.avatar;
    model.type = MineType_avatar;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.title = @"昵称";
    model.detailStr = kUserAccount.user.nickName;
    model.type = MineType_nickName;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.title = @"手机号";
    model.detailStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone"];
    model.type = MineType_phone;
    [self.modelArr addObject:model];
    
    [self.editTV reloadData];
}

#pragma mark - action
- (void)saveImage:(UIImage *)img {
    [YPProgressHud showHudInview:self.view];
    [self.pickManager uploadImage:img success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        NSString *picUrl = responseObject[@"picUrl"];
        if (picUrl.length > 0) {
            MineModel *model = self.modelArr[0];
            model.detailStr = picUrl;
            [self.modelArr replaceObjectAtIndex:0 withObject:model];
            [self.editTV reloadData];
            [self updateAvatar:picUrl];
        }
    } failure:^(NSError * _Nonnull error) {
        kShowText(error.userInfo[kHttpErrorReason]);
        [YPProgressHud hideHudForView:self.view];
    }];
}

- (void)updateAvatar:(NSString *)url {
    [YPProgressHud showHudInview:self.view];
    [NET updateUserInfo:url name:nil success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        kUserAccount.user.avatar = url;
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

- (void)updateNickName:(NSString *)name {
    [YPProgressHud showHudInview:self.view];
    [NET updateUserInfo:nil name:name success:^(id  _Nonnull responseObject) {
        kUserAccount.user.nickName = name;
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditTableViewCell"];
    if (!cell) {
        cell = [[EditTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditTableViewCell"];
    }
    cell.model = self.modelArr[indexPath.row];
    @weakify(self)
    cell.updateNickName = ^(NSString * _Nonnull nickName) {
     @strongify(self)
        [self updateNickName:nickName];
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MineModel *model = self.modelArr[indexPath.row];
    if (model.type == MineType_avatar) {
        [UIAlertController showActionSheetInViewController:self withTitle:nil message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"相册",@"拍照"] tapBlock:^(UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
            if (buttonIndex == UIAlertControllerBlocksFirstOtherButtonIndex) {
                [self.pickManager showPickInVC:self sourceType:UIImagePickerControllerSourceTypePhotoLibrary completion:^(UIImage * _Nullable img) {
                    [self saveImage:img];
                }];
            }else if (buttonIndex == UIAlertControllerBlocksFirstOtherButtonIndex + 1) {
                [self.pickManager showPickInVC:self sourceType:UIImagePickerControllerSourceTypeCamera completion:^(UIImage * _Nullable img) {
                    [self saveImage:img];
                }];
            }
        }];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableView *)editTV {
    if (!_editTV) {
        _editTV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _editTV.backgroundColor = [UIColor whiteColor];
        _editTV.delegate = self;
        _editTV.dataSource = self;
        _editTV.showsVerticalScrollIndicator = NO;
    }
    return _editTV;
}
- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}
- (ImagePickerManager *)pickManager {
    if (!_pickManager) {
        _pickManager = [[ImagePickerManager alloc] init];
    }
    return _pickManager;
}

@end
